package com.changgou.order.task;

import com.alibaba.fastjson.JSON;
import com.changgou.order.config.RabbitMqConfig;
import com.changgou.order.dao.TaskMapper;
import com.changgou.order.pojo.Task;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class QueryPointTask {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Scheduled(cron = "0/2 * * * * ?")
    public void queryTask(){

        //获取小于当前系统时间的数据
        List<Task> taskList = taskMapper.findTaskLessThanCurrentTime(new Date());
        //将任务发送到消息队列上
        if(taskList!=null&&taskList.size()>0){
            for (Task task : taskList) {
                rabbitTemplate.convertAndSend(RabbitMqConfig.EX_BUYING_ADDPOINTUSER,RabbitMqConfig.CG_BUYING_ADDPOINT_KEY, JSON.toJSONString(task));
            }
        }
        System.out.println("5.扫描数据库获取任务消息并向RabbitMQ中发送");
    }
}
